<template>
  <el-dialog title="物料产品选择" width="1200px" :visible.sync="open" @close="handleClose">
    <el-row :gutter="20">
      <el-col :span="4">
        <div class="head-container">
          <el-input v-model="deptName" placeholder="请输入分类名称" clearable prefix-icon="el-icon-search"/>
        </div>
        <div class="head-container">
          <el-tree :data="tree" :props="defaultProps" :expand-on-click-node="false"
                   :filter-node-method="filterNode" ref="tree" node-key="id" default-expand-all highlight-current
                   @node-click="handleNodeClick"/>
        </div>
      </el-col>

      <el-col :span="20">
        <el-form ref="MdItemform" :model="MdItemform" label-width="110px" :inline="true">
          <el-form-item label="物料编码" prop="specification">
            <el-input v-model="MdItemform.itemCode" placeholder="请输入物料编码"/>
          </el-form-item>
          <el-form-item label="物料名称" prop="remark">
            <el-input v-model="MdItemform.itemName" placeholder="请输入物料名称"/>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="getMdItemList">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-table :data="postList" @selection-change="handleSelectionChange" ref="singleTable">
          <el-table-column width="30" align="center">
            <template slot-scope="scope">
              <!-- v-model 绑定选中的行 ID，label 为当前行的唯一标识（如 id） -->
              <el-radio
                v-model="selectedId"
                :label="scope.row.itemId"
                @change="handleRadioChange(scope.row)"
              ></el-radio>
            </template>
          </el-table-column>
          <el-table-column label="物料编码" align="center" prop="itemCode"/>
          <el-table-column label="物料名称" align="center" prop="itemName"/>
          <el-table-column label="规格型号" align="center" prop="specification"/>
          <el-table-column label="单位" align="center" prop="unitOfMeasure"/>
          <el-table-column label="产品物料标识" align="center" prop="itemOrProduct"/>
          <el-table-column label="物料类型名称" align="center" prop="itemTypeName"/>
          <el-table-column label="创建时间" align="center" prop="createTime"/>

        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="MdItemform.pageNum"
                    :limit.sync="MdItemform.pageSize" @pagination="getList"/>

      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>


<script>
import {getMdItemTypeTreeList, getMdItemList, getMdItemByIdList} from "../../../api/system/process";

export default {
  name: "mydialog",
  data() {
    return {
      selectedId: null,
      selectedItem: null,
      open: true,
      deptName: undefined,
      total: 0,
      MdItemform: {
        pageNum: 1,
        pageSize: 10,
        itemCode: undefined,
        itemName: undefined,
      },
      postList: [],
      tree: [],
      defaultProps: {
        children: 'children',
        label: 'itemTypeName'
      }
    }
  },
  watch: {
    // 监听 deptName 的变化
    deptName(val) {
      // 当值变化时，调用树的过滤方法
      this.$refs.tree?.filter(val);
    }
  },
  methods: {
    // 树节点点击事件
    handleNodeClick(data, node, element) {
      // console.log('点击的节点数据：', data); // { id: 1, label: '父节点1', ... }
      // console.log('节点实例信息：', node); // 包含节点层级、展开状态等
      // console.log('节点DOM元素：', element); // <div class="el-tree-node"...>
      // 递归收集所有ID（自身+子节点）
      const getAllIds = (node) => {
        // 先放入当前节点ID
        const ids = [Number(node.itemTypeId)];
        // 若有子节点，递归收集并合并
        if (node.children && node.children.length) {
          node.children.forEach(child => ids.push(...getAllIds(child)));
        }
        return ids;
      };
      // 执行收集
      const allIds = getAllIds(data);
      const allIdsStr = allIds.join(',')
      getMdItemByIdList({allIds:allIdsStr}).then(res=>{
        console.log(res);
        this.postList = res.data
        this.total = res.data.length
      })
    },
    // 树过滤方法
    filterNode(value, data) {
      if (!value) return true
      return data.itemTypeName.indexOf(value) !== -1
    },
    handleRadioChange(row) {
      this.selectedItem = row
    },
    submitForm() {
      this.$emit('submit', this.selectedItem);
      this.$emit('dialog-closed');  // 向父组件发送关闭通知
    },
    cancel() {
      this.$emit('dialog-closed');  // 向父组件发送关闭通知
      this.reset()
    },
    handleQuery() {
    },
    resetQuery() {
      this.MdItemform={
        pageNum: 1,
          pageSize: 10,
          itemCode: undefined,
          itemName: undefined,
      }
      this.getMdItemList()
    },
    handleSelectionChange(selection) {
    },
    getMdItemTypeTreeList() {
      getMdItemTypeTreeList().then(res => {
        this.tree = res.data
      })
    },
    handleClose() {
      this.$emit('dialog-closed');  // 向父组件发送关闭通知
    },
    getMdItemList() {
      getMdItemList(this.MdItemform).then(res => {
        this.postList = res.data
        this.total = res.data.length
      })
    }
  },
  created() {
    this.getMdItemList()
  },
  mounted() {
    this.getMdItemTypeTreeList()
  },
}
</script>

<style scoped>

</style>
